﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace GwenHelpers.Helpers
{
    public static class IQueyableToXmlHelper
    {

        public static XElement simpleGenericToXml<T>(this IEnumerable<T> items)
        {
            if (items.Count() != 0)
            {
                var className = typeof(T).Name;
                var properties = typeof(T).GetProperties();
                var xes = new XElement(className+'s');
                foreach(var item in items){
                    var xe  = new XElement(className); 
                    foreach(var property in properties){
                        if (!property.GetGetMethod().IsVirtual)
                        {
                            xe.Add(new XAttribute(property.Name, (property.GetValue(item)).ToString()));
                        }
                    }
                    xes.Add(xe);
                }
                return xes; 
            }
            else
            {
                return new XElement("test");
            }
        }
    }
}
